# -*- coding: utf-8 -*-
# @Time : 2022/1/10 16:48
# @FileName: StartLoadForm.py
# @Mail: lizhigen1996@aliyun.com

__author__ = 'Zhigen.li'

import sys
import time
import random
import threading
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import pyqtSignal, Qt
from res.resource_rc import *

class StartLoadForm(QWidget):
    closeSignal = pyqtSignal()
    opacitySignal = pyqtSignal(float)

    def __init__(self):
        super(StartLoadForm, self).__init__()
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.BypassWindowManagerHint | Qt.Tool | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        # self.setFixedSize(221 * 1.5, 104 * 1.5)

        self.imgPath = ':/Img/startImg2.png' if random.randint(0, 10) % 2 == 0 else ':/Img/startImg.png'

        self.closeSignal.connect(self.onCloseSignal)
        self.opacitySignal.connect(self.onOpacitySignal)

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHints(QPainter.RenderHint.SmoothPixmapTransform, True)
        qp.drawPixmap(self.rect(), QPixmap(self.imgPath))


    # def show(self):
    #     _t = threading.Thread(target=self.showTimer)
    #     _t.start()
    #     super(StartLoadForm, self).show()

    def closeThread(self):
        _t = threading.Thread(target=self.showTimer)
        _t.start()

    def showTimer(self):
        opacity = 1.0
        while opacity > 0:
            opacity -= 0.01
            self.opacitySignal.emit(opacity)
            time.sleep(0.01)

        self.closeSignal.emit()

    def onCloseSignal(self):
        self.close()

    def onOpacitySignal(self, opacity: float):
        self.setWindowOpacity(opacity)
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 初始化所有视图
    slf = StartLoadForm()
    slf.show()
    slf.closeThread()
    sys.exit(app.exec_())